#include<bits/stdc++.h>
using namespace std;

#define INF 100000000
struct bnode{
  static const int t=20;  //算法导论里面的最小度数
  int n;  //节点关键词个数
  int key[2*bnode::t-1];  //关键字
  int c[2*bnode::t];  //子女
  int siz;
  bool leaf;
}node[10000];
int root; //全局的根
int all=0; //用来分配bnode

void dfs(int r){
  printf("id: %d  leaf: %d siz: %d\n",r,node[r].leaf,node[r].siz);
  printf("key: ");
  for(int i=0;i<node[r].n;i++)
    printf("%d ",node[r].key[i]);
  if(node[r].leaf){printf("\n\n");return ;}
  printf("\nch: ");
  for(int i=0;i<=node[r].n;i++)
    printf("%d ",node[r].c[i]);
  printf("\n\n");
  if(node[r].leaf==false)
    for(int i=0;i<=node[r].n;i++)dfs(node[r].c[i]);
}
void pushup(int r){
  int res=node[r].n;
  if(node[r].leaf==false)
    for(int i=0;i<=node[r].n;i++)
      res+=node[node[r].c[i]].siz;
  node[r].siz=res;
}
//x为非满节点，y=x.c[pos]。y是满节点，分裂y
void split_child(int x,int pos){
  int y=node[x].c[pos];
  ////assert(node[x].n<bnode::t*2-1&&node[y].n==bnode::t*2-1);
  int z=++all; //alloc new node
  //split y
  node[z].leaf=node[y].leaf;
  node[z].n=bnode::t-1;
  for(int i=0;i<node[z].n;i++)
    node[z].key[i]=node[y].key[bnode::t+i];
  if(node[y].leaf==false)
    for(int i=0;i<bnode::t;i++)
      node[z].c[i]=node[y].c[bnode::t+i];
  node[y].n=bnode::t-1;
  //rebuild x
  for(int i=node[x].n;i>pos;i--)
    node[x].c[i+1]=node[x].c[i];
  node[x].c[pos+1]=z;
  for(int i=node[x].n-1;i>=pos;i--)
    node[x].key[i+1]=node[x].key[i];
  node[x].key[pos]=node[y].key[bnode::t-1];
  node[x].n+=1;
  pushup(y);
  pushup(z);
}
void ins_nonfull(int r,int k){
  int i=node[r].n-1;
  if(node[r].leaf==true){
    while(i>=0&&k<node[r].key[i]){
      node[r].key[i+1]=node[r].key[i];
      --i;
    }
    node[r].key[i+1]=k;
    node[r].n+=1;
  }else{
    while(i>=0&&k<node[r].key[i])i--;
    ++i;
    if(node[node[r].c[i]].n==2*bnode::t-1){
      split_child(r,i);
      if(k>node[r].key[i])++i;
    }
    ins_nonfull(node[r].c[i],k);
  }
  //pushup(r);
  ++node[r].siz;
}
void ins(int k){
  int r=root,s;
  if(node[r].n==bnode::t*2-1){
    //root is full
    s=++all;
    root=s;
    node[s].leaf=false;
    node[s].c[0]=r;
    split_child(s,0);
    ins_nonfull(s,k);
  }else {ins_nonfull(r,k);}
}
int pre(int r,int k){
  int i=node[r].n-1;
  while(i>=0&&node[r].key[i]>=k)--i;
  int res=-INF;
  if(i>=0)res=max(res,node[r].key[i]);
  if(node[r].leaf==false)res=max(res,pre(node[r].c[i+1],k));
  return res;
}
int nxt(int r,int k){
  int i=0;
  while(i<node[r].n&&node[r].key[i]<=k)++i;
  int res=INF;
  if(i<node[r].n)res=min(res,node[r].key[i]);
  if(node[r].leaf==false)res=min(res,nxt(node[r].c[i],k));
  return res;
}
//c[x] and c[x+1] has t-1's element
void merge_child(int r,int x){
  //assert(node[r].leaf==false);
  int l,h,k;
  l=node[r].c[x];h=node[r].c[x+1];
  k=node[r].key[x];
  //assert(node[l].n==bnode::t-1);
  //assert(node[h].n==bnode::t-1);
  node[l].key[node[l].n++]=k;
  for(int j=0;j<node[h].n;j++)
    node[l].key[node[l].n+j]=node[h].key[j];
  for(int j=0;j<=node[h].n;j++)
    node[l].c[node[l].n+j]=node[h].c[j];
  node[l].n+=node[h].n;
  for(int j=x;j+1<node[r].n;j++){
    node[r].key[j]=node[r].key[j+1];
    node[r].c[j+1]=node[r].c[j+2];
  }
  node[r].n-=1;
  pushup(l);
  pushup(h);
}
//roll node[r]'s k element's child,
#define LEFT 0
#define RIGHT 1
void roll_child(int x,int k,int dir){
  //assert(node[x].leaf==false);
  int l=node[x].c[k],r=node[x].c[k+1];
  if(dir==LEFT){
    //assert(node[l].n==bnode::t-1&&node[r].n>=bnode::t);
    node[l].key[node[l].n++]=node[x].key[k];
    node[l].c[node[l].n]=node[r].c[0];
    node[x].key[k]=node[r].key[0];
    for(int i=0;i+1<node[r].n;i++)
      node[r].key[i]=node[r].key[i+1];
    for(int i=0;i<node[r].n;i++)
      node[r].c[i]=node[r].c[i+1];
    node[r].n-=1;
  }else{
    //assert(node[r].n==bnode::t-1&&node[l].n>=bnode::t);
    for(int i=node[r].n;i>=1;i--)
      node[r].key[i]=node[r].key[i-1];
    for(int i=node[r].n+1;i>=1;i--)
      node[r].c[i]=node[r].c[i-1];
    node[r].key[0]=node[x].key[k];
    node[r].c[0]=node[l].c[node[l].n];
    node[x].key[k]=node[l].key[node[l].n-1];
    node[l].n-=1;
    node[r].n+=1;
  }
  pushup(l);
  pushup(r);
}
bool find(int r,int k){
  int i=node[r].n-1;
  while(i>=0&&node[r].key[i]>k)--i;
  if(i>=0&&node[r].key[i]==k)return true;
  if(node[r].leaf)return false;
  return find(node[r].c[i+1],k);
}
void del_one(int r,int k){
  int i=node[r].n-1;
  while(i>=0&&node[r].key[i]>k)--i;
  if(i>=0&&node[r].key[i]==k){
    if(node[r].leaf){
      --node[r].n;
      for(;i<node[r].n;i++)node[r].key[i]=node[r].key[i+1];
    }else{
      int x=node[r].c[i];
      if(node[x].n<bnode::t)x=node[r].c[i+1];
      if(node[x].n>=bnode::t){
        int p=(x==node[r].c[i])?pre(x,k+1):nxt(x,k-1);
        node[r].key[i]=p;
        del_one(x,p);
      }else{//c[i] and c[i+1] all has t-1 element
        merge_child(r,i);
        del_one(node[r].c[i],k);
      }
    }
  }else{
    //assert(node[r].leaf==false);
    ++i;
    if(node[node[r].c[i]].n<bnode::t){
      int x=i-1;
      if(x<0||node[node[r].c[x]].n<bnode::t)x=i+1;
      if(x<=node[r].n&&node[node[r].c[x]].n>=bnode::t){
        if(x<i)roll_child(r,x,RIGHT);
        else roll_child(r,i,LEFT);
      }else{
        if(i!=node[r].n)merge_child(r,i);
        else merge_child(r,--i);
      }
    }
    del_one(node[r].c[i],k);
  }
  //pushup(r);
  node[r].siz-=1;
}
void del(int k){
  if(find(root,k)==false)return;
  del_one(root,k);
  if(node[root].n==0)root=node[root].c[0];
}
int rnk(int r,int k){
  int res=0;
  int i;
  for(i=0;i<node[r].n&&node[r].key[i]<k;i++)
    if(node[r].leaf==false){
      res+=node[node[r].c[i]].siz;
    }
  res+=i;
  if(node[r].leaf==false)res+=rnk(node[r].c[i],k);
  return res;
}
int kth(int r,int k){
  if(node[r].leaf){
    //assert(k<=node[r].n);
    return node[r].key[k-1];
  }
  int i=0;
  for(;i<=node[r].n;i++){
    if(node[node[r].c[i]].siz>=k)
      return kth(node[r].c[i],k);
    k-=node[node[r].c[i]].siz;
    if(k==1)return node[r].key[i];
    --k;
  }
  //assert(0);
  return 0;
}
int main(){
  #ifdef BUG
  freopen("a.in","r",stdin);
  #endif

  memset(node,0,sizeof node);
  root=++all; //创建根节点
  node[root].leaf=true;
  ins(INF);
  ins(-INF);
  int t,op,a;
  scanf("%d",&t);
  while(t--){
    scanf("%d%d",&op,&a);
    if(op==1)ins(a);
    else if(op==2)del(a);
    else if(op==3)printf("%d\n",rnk(root,a));
    else if(op==4)printf("%d\n",kth(root,a+1));
    else if(op==5)printf("%d\n",pre(root,a));
    else if(op==6)printf("%d\n",nxt(root,a));
  }
}